<template>
  <div class="swiper" ref="swiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="img in swiperImgData" :key="img.id">
        <img :src="img.imgUrl" :alt="img.id" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper from 'swiper/bundle'
import 'swiper/css/bundle'
export default {
  name: 'zSwiper',
  props: {
    swiperImgData: {
      required: true,
      type: Array,
    },
  },
  watch: {
    swiperImgData: {
      immediate: true,
      async handler(newVal) {
        if (!newVal.length) return
        await this.$nextTick()

        new Swiper(this.$refs.swiper, {
          loop: true,
          autoplay: { disableOnInteraction: false },
          // 如果需要分页器
          pagination: {
            el: '.swiper-pagination',
            clickable: true,
          },
          // 如果需要前进后退按钮
          navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
          },
        })
      },
    },
  },
}
</script>

<style></style>
